package cn.sycoder.mapper;

import cn.sycoder.dto.ProductDto;
import cn.sycoder.dto.ProductFileDto;
import cn.sycoder.dto.ProductNameDto;
import cn.sycoder.entity.Product;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 杨润发
 * @since 2024-10-25
 */
@Repository
public interface ProductMapper extends BaseMapper<Product> {

    @Update("update product set alarm_value = #{alarmValue},alarm = #{alarm} where id = #{id}")
    int update(Long id, Integer alarmValue, Integer alarm);

    @Select("select name, code, inventory, unit, type, location from product")
    List<ProductFileDto> selectProduct();

    // 查询当前库存数量
    @Select("select inventory from product where code = #{code}")
    Integer selectInventory(String code);

    // 通过产品编号查询产品信息
    @Select("select * from product where code = #{code}")
    Product selectProductByCode(String code);

    // 查询入库历史总数
    @Select("select inventory_sum from product where name = #{name}")
    Integer SelectInventorySum(String name);

    // 入库更新库存操作
    @Update("update product set inventory = #{newInventory},inventory_sum = #{newInventorySum} where name = #{name}")
    int updateInventoryInput(String name, Integer newInventory, Integer newInventorySum);

    // 查询出库历史总数
    @Select("select inventory_remove from product where name = #{name}")
    Integer SelectInventoryRemove(String name);

    // 出库更新库存操作
    @Update("update product set inventory = #{newInventory},inventory_remove = #{newInventoryRemove} where name = #{name}")
    int updateInventoryOutput(String name, Integer newInventory, Integer newInventoryRemove);


    @Update("update product set img = #{img} where name = #{name}")
    int saveImageUrl(String img, String name);


    // 按照产品名称进行出库总数排序
    @Select("SELECT name,inventory_remove " +
            "FROM product " +
            "GROUP BY name " +
            "ORDER BY inventory_remove DESC " +
            "LIMIT 5")
    List<ProductNameDto> selectName();


    // 查询产品id和出库总数
    @Select("select id,inventory_sum from product")
    List<ProductNameDto> selectInputCount();

    // 查询产品id和出库总数
    @Select("select id,inventory_remove from product")
    List<ProductNameDto> selectOutputCount();


    @Insert("update product set img = #{url} where code = #{code}")
    int updateUrl(String url, String code);
}
